<?php

namespace App\Http\Controllers\System;

// use Illuminate\Support\Facades\Redis;

use OpenApi\Attributes as OA;


class CaptchaController extends \App\Http\Controllers\Controller {

    #[OA\Get('/admin/captchaImage', '/admin/captchaImage', tags:['后端/账号'], summary:'获取验证码', description:'验证码启用状态后台可控')]
    #[OA\Response(response: 200, description: 'Success、参数/后台异常', content: new OA\JsonContent(
        oneOf:[
            new OA\Schema(title: '请求成功', required: ['code', 'msg', 'captchaEnabled'], properties: [
                new OA\Property('code', title: '状态码', type: 'int', example: 200),
                new OA\Property('msg', title: '请求结果', type: 'string', example: '请求成功 !'),
                new OA\Property('captchaEnabled', title: '验证码是否开启', type: 'bool', example: true),
                new OA\Property('uuid', title: 'UUID', type: 'string', example: '$2y$10$dKJhgqWEzkxo1mDSVRnEferj09veZ7Py2gbt63m5qFcQnEo7ObCa6'),
                new OA\Property('img', title: '图片base64编码', type: 'string', example: 'iVBORw0KGgoAAAANSU...'),
            ]),
            new OA\Schema(ref: '#/components/schemas/Service500ErrBean'),
        ]
    ))]
    #[OA\Response(response: 500, description: '服务器异常', content: new OA\MediaType('text/plain', example: '空/错误堆栈'))]
    public function getCode() {
        $data['captchaEnabled'] = config('admin.admin_catpcha');        // 是否启用验证码

        if ($data['captchaEnabled']) {
            $captcha = app('captcha')->create(api:true);
            $data['uuid']   = $captcha['key'];
            $data['img']    = explode('base64,',$captcha['img'])[1];
        }
        return show_response($data);
    }

}
